<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="priority" label="菜单优先级" align="center"></el-table-column>
                <el-table-column prop="path" label="菜单路径" align="center"></el-table-column>
                <el-table-column prop="description" label="菜单描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="menuDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="级别">
                                    <el-select v-model="formData.level">
                                        <el-option label="主" value="1"></el-option>
                                        <el-option label="副" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单路径">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单优先级">
                                    <el-select v-model="formData.priority">
                                        <el-option label="一" value="1"></el-option>
                                        <el-option label="二" value="2"></el-option>
                                        <el-option label="三" value="3"></el-option>
                                        <el-option label="四" value="4"></el-option>
                                        <el-option label="五" value="5"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单项说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="menuAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData"  label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="级别">
                                    <el-select v-model="formData.level">
                                        <el-option label="主" value="1"></el-option>
                                        <el-option label="副" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="12">
                                <el-form-item label="菜单路径">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单优先级">
                                    <el-select v-model="formData.priority">
                                        <el-option label="一" value="1"></el-option>
                                        <el-option label="二" value="2"></el-option>
                                        <el-option label="三" value="3"></el-option>
                                        <el-option label="四" value="4"></el-option>
                                        <el-option label="五" value="5"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单项说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="menuEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            formValidate:null,
            backend_url:"http://localhost:9002",
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            showMessage(r){
                console.log(r);
                if (r.message.indexOf("403") != -1){
                    this.$message.error("无权访问");
                } else{
                    this.$message.error("无权访问，请登录后再访问！");
                }
            },
            //显示所有数据
            getAllMenu(MenuId){
                console.log("MenuId:"+MenuId);
                axios.get(this.backend_url+"/menu/findAll.do").then((response) => {
                    if(response.data.flag){
                        this.tableData = response.data.data;
                        if(MenuId){
                            // 基于检查组ID获取检查项列表
                            this.getCheckItemByGroupId(MenuId);
                        }
                    }else{
                        this.$message.error(response.data.message);
                    }
                });
            },

            //编辑
            menuEdit() {
                // 表单验证
                this.$refs['dataEditForm'].validate( (valid) => {
                    if (valid) {
                        // 表单验证成功
                        console.log("表单验证成功,发送更新请求");
                        // 表单校验通过，发送请求
                        axios.post(this.backend_url+"/menu/edit.do",this.formData)
                            .then( (response)=>{
                                // 隐藏编辑窗口
                                this.dialogFormVisible4Edit = false;
                                // 编辑成功，弹出成功提示,显示服务返回的消息
                                if (response.data.flag){
                                    this.$message({
                                        message:response.data.message,
                                        type:'success'
                                    });
                                }else{
                                    // 编辑失败，弹出错误提示，显示服务返回的消息
                                    this.$message.error(response.data.message);
                                }
                            })
                            .finally(()=>{
                                //无论执行结果如何，重新发送请求查询分页数据
                                this.findPage();
                            });
                    }else{
                        this.$message.error("表单验证失败");
                    }
                });
            },

            //添加
            menuAdd () {
                //校验表单输入项是否合法
                this.$refs['dataAddForm'].validate((valid)=>{
                    //表单数据校验通过，发送ajax请求将表单数据提交到后台
                    if(valid){
                        console.log("数据验证成功，准备提交请求");
                        // 三个参 URL，表单数据，回调
                        axios.post(this.backend_url+"/menu/add.do",this.formData)
                            .then((response)=> {
                                //隐藏新增窗口
                                this.dialogFormVisible = false;
                                //判断后台返回的flag值，true表示添加操作成功，false为添加操作失败
                                //  保存成功，弹出成功提示，显示服务返回的消息
                                if(response.data.flag){
                                    this.$message({
                                        message:response.data.message,
                                        type:'success'
                                    });
                                }else{
                                    // 保存失败，弹出错误提示，显示服务返回的消息
                                    this.$message.error(response.data.message);
                                }
                            }).finally(()=>{
                            console.log("最终执行");
                            this.findPage();
                        });
                    }else{
                        // 表单验证失败，弹出验证失败提示
                        this.$message.error("数据验证失败");
                    }
                });
            },
            //分页查询
            findPage() {
                // 如果有查询条件，当前页码必须是1
                if(this.pagination.queryString !=null && this.pagination.queryString.length>1){
                    this.pagination.currentPage = 1;
                }
                // 封装请求参数,当前页、默认每页记录数、查询条件
                var params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }
                // 发送axios请求，处理回调
                axios.post(this.backend_url+"/menu/findPage.do",params).then((response)=>{
                    // 把数据集和总记录数赋值给模型
                    this.dataList = response.data.rows;
                    this.pagination.total = response.data.total;
                });
            },

            // 重置表单
            resetForm(formName) {
                this.formData = {};
                // 不为空，清空之前的验证信息
                if( this.$refs[formName] != null){
                    this.$refs[formName].resetFields();
                }
            },

            // 弹出添加窗口
            handleCreate() {

                // 清空上次验证的数据提示
                this.resetForm('dataAddForm');
                // 让窗口可见
                this.dialogFormVisible = true;

            },
            // 弹出编辑窗口
            handleUpdate(row) {
                // 根据ID获取数据
                console.log("row id:"+row.id);
                axios.get(this.backend_url+"/menu/findById.do?id="+row.id).then((response)=>{
                    if(response.data.flag){
                        // 设置编辑窗口属性，dialogFormVisible4Edit为true表示显示
                        this.dialogFormVisible4Edit = true;
                        // 为模型数据设置值，基于VUE双向数据绑定回显到页面
                        this.formData = response.data.data;
                    }else{
                        this.$message.error("获取数据失败，请刷新当前页面");
                    }
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                // 注意改变当前页码
                this.pagination.currentPage = currentPage;
                // 重新执行查询
                this.findPage();
            },

            // 删除
            menuDelete(row) {
                // 提示是否删除
                this.$confirm("确定删除吗?","提示",{type:'warning'}).then(()=>{
                    console.log("row id:"+row.id);
                    // 确认删除，发送axios请求，进行回调处理
                    axios.post(this.backend_url+"/menu/delete.do?id="+row.id).then((response)=>{
                        if (response.data.flag){
                            // 删除成功，提示服务器返回的正常消息
                            this.$message({
                                message:response.data.message,
                                type:'success'
                            });
                            // 刷新当前页面
                            this.findPage();
                        }else{
                            // 删除失败，提示服务器返回的错误消息
                            this.$message.error("删除失败,"+response.data.message);
                        }
                    });
                });
            }
        }
    })
</script>
</html>